using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Globalization;

namespace stsdev {
    class Program {

        [STAThread]
        static void Main(string[] args) {

            Console.WriteLine();
            Console.WriteLine("STSDEV - Simple Tools for SharePoint Developers");
            // Addition 080613 wictor - added output of version number
            Console.WriteLine("Version: " + Type.GetType("stsdev.Program").Assembly.GetName().Version.ToString());
            // End of Addition

            // Added 4/3/2009 - bphillips - Copy registry settings from previous version if needed.
            UpgradeSavedSettings();
            // End of Added 4/3/2009 - bphillips

            // run wizard if when no arguments are passed
            if (args.Length == 0) {
                SolutionBuilder.RunCreateSolutionWizard();
                return;
            }

            // look to first argument to determine operation
            // refresh is the only command supported in version 1.1
            string operation = args[0];
            switch (operation) {
                case "/refresh":
                case "/debug":

                    // Modified 20090722 - bphillips - Add ability to launch the debugger.
                    if (operation.ToLower() == "/debug") {
                        System.Diagnostics.Debugger.Launch();
                    }
                    // End of Modified 20090722

                    if (args.Length < 3) {
                        Console.WriteLine("ERROR - /refresh command requires ConfigurationName, ProjectName, and ProjectDirectory as 2nd, 3rd and 4th arguments");
                    } else if (args.Length == 4) {
                        Console.WriteLine("Refreshing Solution Package Files");
                        // Modified 4/3/2009 - bphillips - Added ConfigurationName parameter 
                        // (Had to add it to front since .Net had trouble parsing command line properly.)
                        string ConfigurationName = args[1].Trim('"');
                        // Modified 3/25/08 by RHoward - Trimmed surrounding quotes to support arguments with spaces
                        string ProjectName = args[2].Trim('"');
                        string ProjectDirectory = args[3].Trim('"');
                        // End of 3/25/08 Modification
                        // End of Modified 4/3/2009 - bphillips
                        SolutionBuilder.ConfigurationName = ConfigurationName;
                        SolutionBuilder.RefreshDeploymentFiles(ProjectName, ProjectDirectory);
                    } else {
                        // Modified 4/3/3009 - bphillips - Gracefully revert to previous behavior if their targets file is outdated.
                        Console.WriteLine("Refreshing Solution Package Files");

                        string ProjectName = args[1].Trim('"');
                        string ProjectDirectory = args[2].Trim('"');

                        SolutionBuilder.ConfigurationName = "Debug";
                        SolutionBuilder.RefreshDeploymentFiles(ProjectName, ProjectDirectory);
                    }

                    break;
                default:
                    Console.WriteLine("ERROR - Unknown command");
                    break;
            }
        }

        // Added 4/3/2009 - bphillips - Copy registry settings from previous version if needed.
        private static void UpgradeSavedSettings() {
            if (Application.UserAppDataRegistry.GetValue(string.Empty) == null) {

                RegistryKey settingsRoot = Registry.CurrentUser.CreateSubKey(@"Software\stsdev\stsdev");

                // Previous version exists.
                if (settingsRoot.SubKeyCount > 1) {
                    RegistryKey previousSettings = settingsRoot.OpenSubKey(settingsRoot.GetSubKeyNames()[settingsRoot.SubKeyCount - 2]);

                    // Copy values from previous settings to current settings.
                    foreach (string settingName in previousSettings.GetSubKeyNames()) {
                        object value = previousSettings.OpenSubKey(settingName).GetValue(string.Empty);
                        RegistryKey key = Application.UserAppDataRegistry.CreateSubKey(settingName);
                        if (value != null) {
                            key.SetValue(string.Empty, value);
                        }
                    }
                }

                Application.UserAppDataRegistry.SetValue(string.Empty, "Upgraded");
            }
        }
    }
}
